<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商品类目" prop="categoryId">
        <treeselect
          style="width: 220px;"
          v-model="queryParams.categoryId"
          :options="categoryList"
          :normalizer="normalizer"
          placeholder="请选择商品类目"
        />
      </el-form-item>
      <el-form-item label="商品标题" prop="subject">
        <el-input v-model="queryParams.subject" placeholder="请输入商品标题" clearable/>
      </el-form-item>
      <el-form-item label="链接" prop="collectUrl">
        <el-input v-model="queryParams.collectUrl" placeholder="请输入商品链接" clearable/>
      </el-form-item>
      <el-form-item label="商品ID" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入商品标题" clearable/>
      </el-form-item>
      <el-form-item label="优化状态" prop="isOptimize">
        <el-select v-model="queryParams.isOptimize" placeholder="请选择优化状态" clearable>
          <el-option v-for="dict in collectList" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="抠图任务" prop="isMatting">
        <el-select v-model="queryParams.isMatting" placeholder="请选择抠图任务" clearable>
          <el-option v-for="dict in dict.type.matting_state" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="指派状态" prop="isCompleteTask">
        <el-select v-model="queryParams.isCompleteTask" placeholder="请选择指派状态" clearable>
          <el-option v-for="dict in dict.type.assignment_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否含有品牌" prop="isBrand">
        <el-select v-model="queryParams.isBrand" placeholder="请选择指派状态" clearable>
          <el-option v-for="dict in dict.type.atlas_has_brand" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="品牌" prop="brandId">
        <el-select v-model="queryParams.brandId" placeholder="请选择品牌" clearable size="small">
          <el-option
            v-for="dict in brandList"
            :key="dict.value"
            :label="dict.lable"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button size="mini" type="primary" @click="autoMatting()">一键抠图</el-button>
      <el-button class="ml20" type="success" size="mini" @click="multiGraphOptimization()">自定义多图片优化</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="collectProductList" :key="isRefreshTable"
              @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column show-overflow-tooltip label="产品id" align="center" prop="id"/>
      <el-table-column show-overflow-tooltip label="商品标题" align="center" prop="subject"/>
      <el-table-column show-overflow-tooltip label="采集链接" align="center" prop="collectUrl">
        <template slot-scope="scope">
          <a :href="scope.row.collectUrl" style="color: #1890ff;" target="_blank">{{ scope.row.collectUrl }}</a>
        </template>
      </el-table-column>
      <el-table-column label="商品图" align="center" prop="white" width="150">
        <template slot-scope="scope">
          <imagePreview :src="scope.row.white" width="60px" height="60px"/>
        </template>
      </el-table-column>
      <el-table-column label="商品优化状态" align="center" prop="isOptimize" width="150">
        <template slot-scope="scope">
          <div v-if="scope.row.expand && scope.row.expand.isOptimize == 1">已优化</div>
          <div v-else>未优化</div>
        </template>
      </el-table-column>
      <el-table-column label="抠图状态" align="center" prop="isMatting" width="150">
        <template slot-scope="scope">
          <div v-if="scope.row.optimizeTask && scope.row.optimizeTask.status == 1">已完成自动抠图</div>
          <div v-else-if="scope.row.optimizeTask && scope.row.optimizeTask.status == 0">已加入自动抠图</div>
          <div v-else>未自动抠图</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" v-if="!scope.row.optimizeTask" type="text" @click="autoMatting(scope.row)">
            一键抠图
          </el-button>
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">编辑图片</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <el-dialog
      title="填写测评信息"
      :visible.sync="isOpenMultiGraphModal"
      width="500px"
      :before-close="closeModal">
      <div style="display: flex">
        <div style="width: 100px;text-align: right;font-size: 16px">图集ID：</div>
        <el-input
          type="textarea"
          :autosize="{ minRows: 3, maxRows: 10}"
          placeholder="请输入图集ID"
          v-model="collectProductIds">
        </el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeModal">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </span>
    </el-dialog>
    <editPicture v-if="isShowEditPicture" :collectProductId="collectProductId" @imgDetailSwitch="imgDetailSwitch"/>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import {listCollectProduct, delCollectProduct, getBrand} from "@/api/gather/collectProduct";
import {addToOptimizeTask, collectProductOptimize} from "@/api/picture/picture";
import editPicture from "@/views/picture/editPicture.vue";
import {selectlistCollectProductCategory} from "@/api/gather/collectProductCategory";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  components: {
    editPicture,
    Treeselect
  },
  dicts: ['matting_state', 'assignment_status', 'atlas_has_brand'],
  data() {
    return {
      categoryList: [],// 商品类目
      loading: true,  // 遮罩层
      ids: [], // 选中数组
      single: true,   // 非单个禁用
      multiple: true,// 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      collectProductList: [],// 采集商品表格数据
      title: "",      // 弹出层标题
      open: true,      // 是否显示弹出层
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isOptimize: null,
        isMatting: "",//抠图状态
        isCompleteTask: "",//指派状态
        isBrand: "",
        isEdit: 1,
        id: "",//商品id
        collectUrl: "",//采集链接
        subject: "",//商品标题
        type: 2
      },   // 查询参数
      collectList: [// 优化状态
        {
          value: 0,
          label: '未优化'
        },
        {
          value: 1,
          label: '已优化'
        }
      ],
      isShowEditPicture: false,//是否显示编辑图片弹窗
      collectProductId: "",//采集商品ID
      isRefreshTable: false,//是否刷新表格
      isOpenMultiGraphModal: false,//是否打开多图优化弹窗
      collectProductIds: '',
      brandList: [],//品牌列表
    };
  },
  created() {
    this.getList();
    this.getGoodsCategory()
    this.getBrandList()
  },
  methods: {
    // 获取品牌选择列表
    getBrandList() {
      getBrand().then(res => {
        this.brandList = res.data
      });
    },
    // 商品类目
    getGoodsCategory() {
      this.loading = true;
      selectlistCollectProductCategory().then(response => {
        this.categoryList = this.handleTree(response.data, "id", "parentId");
      });
      this.loading = false;
    },
    /** 转换采集商品类目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    confirm() {
      let data = {
        collectProductIds: this.collectProductIds
      }
      collectProductOptimize(data).then(res => {
        this.$message.success(res.msg)
        this.closeModal()
      })
    },
    // 多图优化
    multiGraphOptimization() {
      this.isOpenMultiGraphModal = true
    },
    // 关闭多图优化弹窗
    closeModal() {
      this.isOpenMultiGraphModal = false
      this.getList()
    },
    autoMatting(row) {
      if (!row && this.ids && this.ids.length < 1) {
        this.$message.error('请至少选择一种产品')
        return
      }
      this.$confirm("是否执行一键抠图操作？", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let productId = this.ids.toString()
        let data = {
          collectProductIds: row ? row.id : productId
        }
        addToOptimizeTask(data).then(response => {
          this.$message.info(response.msg)
          // this.isRefreshTable = !this.isRefreshTable
          this.getList()
        });
      })
    },
    // 图片编辑弹窗开关
    imgDetailSwitch(e) {
      this.isShowEditPicture = e
      this.getList()
    },
    /** 查询采集商品列表 */
    getList() {
      this.loading = true;
      listCollectProduct(this.queryParams).then(response => {
        this.collectProductList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push(`/gather/details/` + 'add')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.collectProductId = row.id
      this.isShowEditPicture = true
      // this.$router.push(`/gather/details/` + row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除采集商品编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delCollectProduct(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    }
  }
};
</script>

